package dao.bis;

import dao.IBoardDao;
import entity.Board;
import util.BaseDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BoardDao extends BaseDB implements IBoardDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //获取板块页数
    @Override
    public List<Board> getBoardByPage(int pageNum, int pageSize) {
        List<Board> boards = new ArrayList<>();
        try {
            conn = getConn();
            String sql = "select * from tb_board limit ?,?";
            ps = conn.prepareStatement(sql);
            /*pageSize取多少条
            pageNum每页的开始位置
             */
            ps.setInt(1,pageSize*(pageNum-1));
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                Board board = new Board(rs.getInt("bid"),rs.getString("bname"));
                boards.add(board);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,ps,rs);
        }
        return boards;
    }
    //获取板块数量
    @Override
    public int getAllBoardCount() {
        int count =0;
        try {
            conn =getConn();
            String sql = "select count(1) from tb_board";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()){
                count =rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,ps,rs);
        }
        return count;
    }

}
